<template>
  <div class="index">
    <van-tabs v-model="active">
      <van-tab v-for="(item, index) in titles" :key="index" :title="item.name">
        <article-list :channel="item"></article-list>
      </van-tab>
    </van-tabs>
    <!--1. tab -->
    <div class="bar-btn" @click="isShowChannelEdit = true">
      <van-icon name="wap-nav" />
    </div>
    <!--2. list -->
    <van-action-sheet v-model="isShowChannelEdit" title="标题">
      <channel-edit
        :channels="titles"
        :curIdx="active"
        @change-channel="hChangeChannel"
        @fixed-active-index="hFixedActiveIndex"
      ></channel-edit>
    </van-action-sheet>
  </div>
</template>

<script>
import ChannelEdit from './channelEdit'
import ArticleList from './articleList.vue'
import { getChannel } from '../../api/channel'
export default {
  name: 'home',
  data () {
    return {
      active: 1,
      titles: [],
      isShowChannelEdit: false
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  methods: {
    async getTitles () {
      try {
        const res = await getChannel()
        console.log(res)
        console.log(res.data.data.channels)
        this.titles = res.data.data.channels
      } catch (err) {
        console.log('错误是因为', err)
      }
    },
    hChangeChannel (idx) {
      // 1. 关闭弹层
      this.isShowChannelEdit = false
      // 2. 切换频道
      this.active = idx
      console.log('收到了子组件中的频道切换事件,下标是', idx)
    },
    hFixedActiveIndex () {
      this.active--
    }

  },
  created () {
    this.getTitles()
  }

}
</script>
<style lang="less" scoped>
.bar-btn {
  position: fixed;
  right: 5px;
  top: 57px;
  display: flex;
  align-items: center;
  background-color: #fff;
  opacity: 0.8;
  z-index: 1;
  .van-icon-wap-nav {
    font-size: 20px;
  }
}
</style>
